package org.rqb.panel;

import lombok.extern.slf4j.Slf4j;
import org.rqb.Constant;
import org.rqb.config.MyConfig;
import org.rqb.frame.MyFrame;
import org.rqb.ui.button.MyTextureButton;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import static org.rqb.Constant.CONFIG_SIGN_FILE_DIR;

@Slf4j
public class SignPanel extends JPanel implements ActionListener {
    private MyFrame frame;
    private JPanel cardPanel;
    private CardLayout cardLayout;
    private SignCenterPanel signCenterPanel;



    private MyTextureButton undoBtn;  // 上一步按钮
    private MyTextureButton redoBtn;  // 下一步按钮
    private String defaultFileName = "mySign";
    private JPanel topPanel;
    private JPanel foolPanel;
    private MyTextureButton resetBtn;
    private MyTextureButton saveBtn;
    private JLabel saveFileDirLabel;
    private MyTextureButton saveFileBtn;
    private JPanel foolCenterPanel;

    private GridBagConstraints gbc;
    private JLabel saveFileNameLable;
    private JTextPane saveFileNameText;
    private JLabel saveSuffixLable;

    public SignPanel(JPanel cardPanel, CardLayout cardLayout, MyFrame myFrame) {
        this.cardPanel = cardPanel;
        this.cardLayout = cardLayout;
        this.frame = myFrame;

        panelInit();
        btnInit();

        // 确保布局重新计算和更新
        revalidate();
        repaint();
    }

    private void btnInit() {
        undoBtn = new MyTextureButton("上一步");
        redoBtn = new MyTextureButton("下一步");
        resetBtn = new MyTextureButton("清空");
        saveBtn = new MyTextureButton("保存");
        undoBtn.addActionListener(this);
        redoBtn.addActionListener(this);
        resetBtn.addActionListener(this);
        saveBtn.addActionListener(this);

        //文件保存选择按钮
        saveFileBtn = new MyTextureButton("选择目录：");
        saveFileDirLabel = new JLabel(MyConfig.getConfig().getProperty(CONFIG_SIGN_FILE_DIR));
        saveFileBtn.addActionListener(this);

        saveFileNameLable = new JLabel("文件名：");
        saveFileNameText = new JTextPane();
        saveFileNameText.setText(defaultFileName);
        saveSuffixLable = new JLabel(".png");


        // 添加第一个组件
        gbc.gridx = 0;
        gbc.gridy = 0;
        foolCenterPanel.add(saveFileBtn, gbc);

        // 添加第二个组件
        gbc.gridx = 1;
        gbc.gridy = 0;
        foolCenterPanel.add(saveFileDirLabel, gbc);

        // 添加第三个组件
        gbc.gridx = 0;
        gbc.gridy = 1;
        foolCenterPanel.add(saveFileNameLable, gbc);

        // 添加第四个组件
        gbc.gridx = 1;
        gbc.gridy = 1;
        foolCenterPanel.add(saveFileNameText, gbc);
        // 添加第五个组件
        gbc.gridx = 2;
        gbc.gridy = 1;
        foolCenterPanel.add(saveSuffixLable, gbc);



        JPanel leftPanel = new JPanel();
        leftPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); // 上一步的按钮
        leftPanel.add(undoBtn);

        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); // 下一步的按钮
        rightPanel.add(redoBtn);

        JPanel leftPanel1 = new JPanel();
        leftPanel1.setLayout(new FlowLayout(FlowLayout.LEFT)); // 清空的按钮
        leftPanel1.add(resetBtn);

        JPanel rightPanel1 = new JPanel();
        rightPanel1.setLayout(new FlowLayout(FlowLayout.RIGHT)); // 保存的按钮
        rightPanel1.add(saveBtn);



        // 添加左侧和右侧面板到 topPanel
        topPanel.add(leftPanel, BorderLayout.WEST);
        topPanel.add(rightPanel, BorderLayout.EAST);
        foolPanel.add(leftPanel1,BorderLayout.WEST);
        foolPanel.add(rightPanel1,BorderLayout.EAST);


    }

    private void panelInit() {
        this.setLayout(new BorderLayout());

        // 创建 SignCenterPanel 并设置推荐尺寸
        signCenterPanel = new SignCenterPanel(this.cardPanel, this.cardLayout, this.frame);
        signCenterPanel.setPreferredSize(new Dimension(Constant.FRAME_WIDTH, Constant.FRAME_HEIGHT - 200)); // 设置推荐尺寸

        //创建底部中间JPanel
        foolCenterPanel = new JPanel(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.CENTER;

        // 创建顶部和底部面板
        topPanel = new JPanel();
        topPanel.setPreferredSize(new Dimension(Constant.FRAME_WIDTH, 100)); // 设置推荐尺寸

        foolPanel = new JPanel();
        foolPanel.setPreferredSize(new Dimension(Constant.FRAME_WIDTH, 100)); // 设置推荐尺寸

        // 使用 BorderLayout 布局管理器
        topPanel.setLayout(new BorderLayout());
        foolPanel.setLayout(new BorderLayout());

        foolPanel.add(foolCenterPanel, BorderLayout.CENTER);

        // 添加面板到 SignPanel
        add(signCenterPanel, BorderLayout.CENTER);
        add(topPanel, BorderLayout.NORTH);  // 顶部面板应该在北边
        add(foolPanel, BorderLayout.SOUTH); // 底部面板应该在南边


        //更新配置文件中默认存储路径
        updateSignFileSaveDir(Constant.USER_HOME);
    }

    /**
     * 更新默认的签名存储路径
     * @param home 用户目录
     */
    private void updateSignFileSaveDir(String home) {
        String saveDir = MyConfig.getConfig().getProperty(CONFIG_SIGN_FILE_DIR);
        if (saveDir == null || "".equals(saveDir.trim())) {

            HashMap<String, String> map = new HashMap<>();
            map.put(CONFIG_SIGN_FILE_DIR,home + "\\Desktop");
            MyConfig.updateConfig(map);
        }

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == undoBtn) {
           undoBtnOnClick();
        } else if (e.getSource() == redoBtn) {
            // 处理“下一步”按钮的事件
            redoBtnOnClick();
        } else if (e.getSource() == resetBtn) {
            resetBtnOnClick();
        } else if (e.getSource() == saveBtn) {
            //保存
//            String deskTop = Constant.USER_HOME + "\\desktop";


            SwingWorker<Void, Void> swingWorker = new SwingWorker<Void, Void>() {
                @Override
                protected Void doInBackground() throws Exception {
                    //获取保存路径
                    String saveDir = saveFileDirLabel.getText();
                    //获取保存文件名
                    String saveFileName = saveFileNameText.getText().trim();
                    if ("".equals(saveFileName)) {
                        //弹出提示框
                        JOptionPane.showMessageDialog(frame, "文件名不能为空", "错误", JOptionPane.ERROR_MESSAGE);

                        //结束
                        return null;
                    }

                    signCenterPanel.saveSignatureToFile(saveDir, saveFileName + ".png");

                    //将文件保存路径存储到配置文件中
                    updateConfig(CONFIG_SIGN_FILE_DIR,saveDir);



                    return null;
                }
            };
            swingWorker.execute();
        } else if (e.getSource() == saveFileBtn) {
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int returnValue = chooser.showOpenDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                saveFileDirLabel.setText(chooser.getSelectedFile().getAbsolutePath());
            }

        }
    }

    private void updateConfig(String key,String value) {
        HashMap<String, String> map = new HashMap<>();
        map.put(key,value);
        MyConfig.updateConfig(map);
    }

    private void resetBtnOnClick() {
        signCenterPanel.reset();
    }

    private void redoBtnOnClick() {
        signCenterPanel.redoLast();

    }

    private void undoBtnOnClick() {
        signCenterPanel.undoLast();
    }



    private String getUserHomeDir() {
        return System.getProperty("user.home");
    }

}
